package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.app.util.UploadUtil;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("images")
public class ImageController {

    @Autowired
    private ServletContext ctx;

    @PostMapping//上传方法,将文件保存到指定路径,并且修改名字
    public  Object upload(MultipartFile file){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //先获得/upload的真实路径,然后将上传文件保存到/upload路径
            //设置共享域
            //String url = UploadUtil.upload(file, SevenUploadUtil.uploadQiniu(file));
            String url = SevenUploadUtil.uploadQiniu(file);
            //上传成功状态为 1
            map.put("url", url);
            map.put("status", 1);
        }catch (Exception e){
            map.put("status", 0);
            map.put("msg", e.getMessage());
        }
        return map;
    }
}
